﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLM.Domain.DTO;
using DLM.Data.EF.CodeFirst.Repositories;
using System.Web.Mvc;
using System.Web;
using DLM.Infrastructure.Cache;
using DLM.Domain.Model;

namespace DLM.Services.Implementations
{
    public class PageDataService
    {
        public static IEnumerable<BillBoard> BillboardList()
        {
            var cacheResult = CacheHelper.GetFromCache<IEnumerable<BillBoard>>("AllBillBoard");
            if (cacheResult == null)
            {
                var list = new PageDataRepository().BillboardList();
                CacheHelper.SaveToCache<IEnumerable<BillBoard>>("AllBillBoard", list);
                return list;
            }
            return cacheResult;
      
        }

        public static IEnumerable<BillBoard> TopBillBoard()
        {
            var cacheResult = CacheHelper.GetFromCache<IEnumerable<BillBoard>>("TopBillBoard");
            if (cacheResult == null)
            {
                var list = new PageDataRepository().TopBillboardList();
                CacheHelper.SaveToCache<IEnumerable<BillBoard>>("TopBillBoard", list);
                return list;
            }
    

            return cacheResult;
        }

        /*
        public static ProvinceDTO ShowProvinceData()
        {
            var cachedCountry = CacheHelper.GetFromCache <IEnumerable<Country>>("Country");
            ProvinceDTO cachedDTO = CacheHelper.GetFromCache<ProvinceDTO>("CountryDTO");
            if ((cachedCountry == null) || (cachedDTO == null))
            {
                var list = new PageDataRepository().FindCountries();
                CacheHelper.SaveToCache<IEnumerable<Country>>("Country", list);

                ProvinceDTO dto = new ProvinceDTO();
                dto.City = list
                    .Select(c => c.City)
                    .Distinct()
                    .Select(c => new SelectListItem { Text = c, Value = c })
                    .Distinct()
                    .ToList<SelectListItem>();
                dto.Province = list
                    .Select(c => c.Province)
                    .Distinct()
                    .Select(c => new SelectListItem { Text = c, Value = c })
                    .Distinct()
                    .ToList<SelectListItem>();
                dto.Zone = list
                    .Select(c => c.Zone).Distinct()
                    .Select(c => new SelectListItem { Text = c, Value = c })
                    .Distinct()
                    .ToList<SelectListItem>();
                dto.Town = list
                    .Select(c => c.Town)
                    .Distinct()
                    .Select(c => new SelectListItem { Text = c, Value = c })
                    .Distinct()
                    .ToList<SelectListItem>();
                CacheHelper.SaveToCache<ProvinceDTO>("CountryDTO", dto);
                return dto;
            }
            return cachedDTO;
        }

        public static List<SelectListItem> ShowCity(string province)
        {
            var cachedCountry = CacheHelper.GetFromCache<IEnumerable<Country>>("Country");
            if (cachedCountry == null)
            {
                var list = new PageDataRepository().FindCountries();
                CacheHelper.SaveToCache<IEnumerable<Country>>("Country", list);
                List<SelectListItem> cityList = list.Where(p => p.Province == province)
                    .Select(c => c.City).Distinct()
                    .Select(c => new SelectListItem { Text = c, Value = c })
                    .ToList<SelectListItem>();
                return cityList;
            }
            else
            {
                List<SelectListItem> cityList = cachedCountry.Where(p => p.Province == province)
                    .Select(c => c.City).Distinct()
                    .Select(c => new SelectListItem { Text = c, Value = c })
                    .ToList<SelectListItem>();
                return cityList;
            }
            return null;

        }


        public static List<SelectListItem> ShowZone(string city)
        {
            var cachedCountry = CacheHelper.GetFromCache<IEnumerable<Country>>("Country");
            if (cachedCountry == null)
            {
                var list = new PageDataRepository().FindCountries();
                CacheHelper.SaveToCache<IEnumerable<Country>>("Country", list);
                List<SelectListItem> cityList = list.Where(p => p.City == city)
                    .Select(c => c.Zone).Distinct()
                    .Select(c => new SelectListItem { Text = c, Value = c })
                    .ToList<SelectListItem>();
                return cityList;
            }
            else
            {
                List<SelectListItem> cityList = cachedCountry.Where(p => p.City == city)
                    .Select(c => c.Zone).Distinct()
                    .Select(c => new SelectListItem { Text = c, Value = c })
                    .ToList<SelectListItem>();
                return cityList;
            }
            return null;

        }
        */


        public static List<SelectListItem> ShowProvince()
        {
            var cachedProvince = CacheHelper.GetFromCache<List<SelectListItem>>("Province");
            if (cachedProvince == null)
            {
                var list = new PageDataRepository().FindCountry();
                
                var result = list
                    .Select(p => new SelectListItem { Text = p.ProvinceName, Value = p.ProvinceName })
                    .ToList<SelectListItem>();
                CacheHelper.SaveToCache<List<SelectListItem>>("Province", result);
                return result;
            }
            return cachedProvince;

        }

        public static List<SelectListItem> ShowCity(string province)
        {
            var list = new PageDataRepository().FindCity(province);

            var result = list
                .Select(p => new SelectListItem { Text = p.CityName, Value = p.CityName })
                .ToList<SelectListItem>();
            return result;
        }

        public static List<SelectListItem> ShowDistrict(string city)
        {
            var list = new PageDataRepository().FindDistrict(city);

            var result = list
                .Select(p => new SelectListItem { Text = p.DistrictName, Value = p.DistrictName })
                .ToList<SelectListItem>();
            return result;
        }


        public static int PartyInvitationCount(string accountName)
        {
            int count = new PageDataRepository().FindPartyRequestCount(accountName);
            return count;
        }


        public static int FriendInvitationCount(string accountName)
        {
            int count = new PageDataRepository().FindFriendRequestCount(accountName);
            return count;
        }

        public static int GreetInvitationCount(string accountName)
        {
            int count = new PageDataRepository().FindGreetRequestCount(accountName);
            return count;
        }

    }
}
